using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
	public static partial class IconService
	{
        public static bool AddIcon(Icon icon)
		{
            string sql =
				"INSERT Icon (Title, Image)" +
				"VALUES (@Title, @Image)";
				
			sql += " ; SELECT @@IDENTITY";

            
			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@Title", icon.Title),
				new SqlParameter("@Image", icon.Image)
			};
			
            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
			if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
		}
		
		
        public static bool DeleteIconByID(int iD)
		{
            string sql = "DELETE Icon WHERE ID = @ID";

           	SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
		
            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
		}
		
        public static bool ModifyIcon(Icon icon)
        {
            string sql =
                "UPDATE Icon " +
                "SET " +
	                "Title = @Title, " +
	                "Image = @Image " +
                "WHERE ID = @ID";

			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", icon.ID),
				new SqlParameter("@Title", icon.Title),
				new SqlParameter("@Image", icon.Image)
			};

			int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		

        public static IList<Icon> GetAllIcons()
        {
            string sqlAll = "SELECT * FROM Icon";
			return GetIconsBySql( sqlAll );
        }
		
        public static Icon GetIconByID(int iD)
        {
            string sql = "SELECT * FROM Icon WHERE ID = @ID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
			
            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);
			
			Icon icon = new Icon();
			
            foreach (DataRow dr in temp.Rows)
            {
				icon.ID = (int)dr["ID"];
				icon.Title = (string)dr["Title"];
				icon.Image = (string)dr["Image"];
				
            }
            return icon;    
            
        }
		
		
        private static IList<Icon> GetIconsBySql(string safeSql)
        {
            List<Icon> list = new List<Icon>();

			using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, null))
			{
				while (dr.Read())
				{
					Icon icon = new Icon();
					
					icon.ID = (int)dr["ID"];
					icon.Title = (string)dr["Title"];
					icon.Image = (string)dr["Image"];
	
					list.Add(icon);
				}
			}
            return list;
        }
		
        private static IList<Icon> GetIconsBySql(string safeSql, params SqlParameter[] para)
        {
            List<Icon> list = new List<Icon>();

			using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, para))
			{
				while (dr.Read())
				{
					Icon icon = new Icon();
					
					icon.ID = (int)dr["ID"];
					icon.Title = (string)dr["Title"];
					icon.Image = (string)dr["Image"];
	
					list.Add(icon);
				}
			}
            return list;
        }
	}
}
